package com.zbf.task.config;

import com.alibaba.fastjson.JSON;
import com.zbf.task.mapper.TestOrderMapper;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/12  14:43
 * 描述:
 */
@Component
public class MyMsgConfirmCallBack implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
    }

    @Autowired
    private TestOrderMapper testOrderMapper;

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        //获取消息信息
        byte[] body = correlationData.getReturnedMessage().getBody();

        String orderMsg = new String(body);

        Map<String,Object> map = JSON.parseObject(orderMsg, Map.class);

        if(b){
            Map<String, Object> orderInfo = testOrderMapper.getOrderInfo(map.get("id").toString());
            if(!orderInfo.get("state").toString().equals("4")){
                map.put("state",2);
                testOrderMapper.updateOrder(map);
            }
        }else{
            map.put("state",3);
            testOrderMapper.updateOrder(map);
        }

    }
}
